//
//  WXPOrderListCell.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/3.
//
//

#import "WXPOrderListCell.h"

@interface WXPOrderListCell ()

@end

@implementation WXPOrderListCell

#pragma mark - lazy
- (UIView *)viewBack
{
    if (_viewBack == nil) {
        _viewBack = [UIView  new];
        _viewBack.backgroundColor = COLOR_WHITE;
        _viewBack.widthHeight = XY(SCREEN_WIDTH-LEFT_MARDIN*2, 0);
        [GlobalMethod setRoundView:_viewBack color:[UIColor clearColor] numRound:16 width:0];
        _viewBack.userInteractionEnabled = true;
    }
    return  _viewBack;
}
- (UIImageView *)imageLogo{
    if (_imageLogo == nil) {
        _imageLogo = [UIImageView new];
        _imageLogo.widthHeight = XY(36, 36);
        [GlobalMethod setRoundView:_imageLogo color:[UIColor clearColor] numRound:8 width:0];
    }
    return _imageLogo;
}
- (UILabel *)labelTitle{
    if (_labelTitle == nil) {
        _labelTitle = [UILabel new];
        _labelTitle.textColor = COLOR_BLACK;
        _labelTitle.font = [UIFont boldSystemFontOfSize:14];
        _labelTitle.numberOfLines = 1;
    }
    return _labelTitle;
}
- (UILabel *)labelState{
    if (_labelState == nil) {
        _labelState = [UILabel new];
        _labelState.textColor = COLOR_CONTENT;
        _labelState.fontNum = 12;
        _labelState.textAlignment = NSTextAlignmentCenter;
    }
    return _labelState;
}
- (UILabel *)labelMoneyTitle{
    if (_labelMoneyTitle == nil) {
        _labelMoneyTitle = [UILabel new];
        _labelMoneyTitle.textColor = COLOR_BLACK;
        _labelMoneyTitle.fontNum = 12;
        [_labelMoneyTitle fitTitle:@"Importe del préstamo" variable:0];
    }
    return _labelMoneyTitle;
}
- (UILabel *)labelMoney{
    if (_labelMoney == nil) {
        _labelMoney = [UILabel new];
        _labelMoney.textColor = COLOR_BLACK;
        _labelMoney.font = [UIFont boldSystemFontOfSize:20];
    }
    return _labelMoney;
}
- (UILabel *)labelTimeTitle{
    if (_labelTimeTitle == nil) {
        _labelTimeTitle = [UILabel new];
        _labelTimeTitle.textColor = COLOR_BLACK;
        _labelTimeTitle.fontNum = 12;
        [_labelTimeTitle fitTitle:@"Fecha de vencimiento" variable:0];
    }
    return _labelTimeTitle;
}
- (UILabel *)labelTime{
    if (_labelTime == nil) {
        _labelTime = [UILabel new];
        _labelTime.textColor = COLOR_BLACK;
        _labelTime.fontNum = 14;
    }
    return _labelTime;
}
- (UILabel *)labelError{
    if (_labelError == nil) {
        _labelError = [UILabel  new];
        _labelError.numberOfLines = 0;
        _labelError.lineSpace = 2;
        _labelError.textColor = COLOR_RED;
        _labelError.fontNum = 12;
        _labelError.width = SCREEN_WIDTH-LEFT_MARDIN*4;
        NSString * str = @"  El pedido actual se encuentra atrasado en cuanto al reembolso, por favor, haga el reembolso a tiempo para evitar cargos adicionales";
        [_labelError setAttributedText:[str setImageName:@"icon-order-error" imageRect:CGRectMake(0, -3.5, 16, 17) withIndex:0]];
        [_labelError sizeToFit];
        _labelError.hidden = true;
    }
    return  _labelError;
}
- (UILabel *)labelHint{
    if (!_labelHint) {
        _labelHint = [UILabel new];
        _labelHint.numberOfLines = 0;
        _labelHint.lineSpace = 2;
        _labelHint.textColor = [UIColor colorWithHexString:@"#FF8420"];
        _labelHint.fontNum = 12;
        [_labelHint fitTitle:@"Le pedimos que compruebe su cuenta bancaria, podrá continuar solicitando productos después de la actualización" variable:SCREEN_WIDTH-LEFT_MARDIN*4];
        _labelHint.hidden = true;
    }
    return _labelHint;
}
- (UIButton *)btnEdit{
    if (_btnEdit == nil) {
        _btnEdit = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnEdit.tag = 1;
        [_btnEdit addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _btnEdit.backgroundColor = COLOR_PRIMARY;
        _btnEdit.titleLabel.fontNum = 14;
        [_btnEdit setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        [_btnEdit setTitle:@"Ir a modificar" forState:(UIControlStateNormal)];
        _btnEdit.widthHeight = XY(self.viewBack.width-2*LEFT_MARDIN, BUTTON_HEIGHT);
        [GlobalMethod setRoundView:_btnEdit color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
        _btnEdit.hidden = true;
    }
    return _btnEdit;
}
- (NSString *)moneyStr{
    double applyTime = self.orderModel.beforePaymentDetail.applyTime;
    double repaymentTime = self.orderModel.repaymentDetail.repaymentTime;
    double repayTime = self.orderModel.settledLoanDetail.repayTime;

    if(applyTime > 0){
        return [GlobalMethod showMoney:strDot2F(self.orderModel.beforePaymentDetail.loanAmount)];
    }else{
        if (repaymentTime > 0){
            return [GlobalMethod showMoney:strDot2F(self.orderModel.repaymentDetail.repaymentAmount)];
        }else{
            if (repayTime > 0){
                return [GlobalMethod showMoney:strDot2F(self.orderModel.settledLoanDetail.repaymentIncomeAmount)];
            }else{
                return @"0";
            }
        }
    }
    return @"";
}

- (NSString *)timeStr{
    double applyTime = self.orderModel.beforePaymentDetail.applyTime;
    double repaymentTime = self.orderModel.repaymentDetail.repaymentTime;
    double repayTime = self.orderModel.settledLoanDetail.repayTime;

    if(applyTime > 0){
        return [GlobalMethod exchangeTimestamp:strDotF(self.orderModel.beforePaymentDetail.applyTime)  formate:TIME_DAY_MONTH_YEAR];
    }else{
        if (repaymentTime > 0){
            return [GlobalMethod exchangeTimestamp:strDotF(self.orderModel.repaymentDetail.repaymentTime) formate:TIME_DAY_MONTH_YEAR];
        }else{
            if (repayTime > 0){
                return [GlobalMethod exchangeTimestamp:strDotF(self.orderModel.settledLoanDetail.repayTime) formate:TIME_DAY_MONTH_YEAR];
            }else{
                return @"";
            }
        }
    }
    return  @"";
}
- (NSString *)stateStr
{
    switch ([self.orderModel.status integerValue]) {
        case 90:
        case 100:
        {
            // 审核中和审核通过
            return @"En revisión";
        }
            break;
        case 110:
        {
            // 审核拒绝
            return @"No ha pasado la revisión";
        }
            break;
        case 150:
        {
            // 放款中
            return @"Depositando";
        }
            break;
        case 169:
        {
            // 放款失败
            return @"Depósito fallido";
        }
            break;
        case 170:
        {
            // 放款成功
            return @"Pendiente de reembolso";
        }
            break;
        case 180:
        {
            // 订单逾期
            return @"Reembolso atrasado";
        }
            break;
        case 200:
        {
            // 订单结清(已还)
            return @"Reembolsado";
        }
            break;
            
        default:
        {
            return @"";
        }
            break;
    }
}
- (UIColor *)stateColor{
    switch ([self.orderModel.status integerValue]) {
        case 180:
        {
            // 订单逾期
            return COLOR_RED;
        }
            break;
            
        default:
        {
            return COLOR_CONTENT;
        }
            break;
    }
}
- (UIColor *)stateBackColor{
    switch ([self.orderModel.status integerValue]) {
        case 180:
        {
            // 订单逾期
            return [UIColor colorWithHexString:@"#E84A55" alpha:0.2];
        }
            break;
            
        default:
        {
            return COLOR_LINE;
        }
            break;
    }
}
- (NSString *)timeTitleStr{
    switch ([self.orderModel.status integerValue]) {
        case 90:
        case 100:
        case 110:
        case 150:
        case 169:
        {
            // 审核中和审核通过
            return @"Fecha de solicitud";
        }
            break;
        case 170:
        case 180:
        {
            return @"Fecha de vencimiento";
        }
            break;
        case 200:
        {
            // 订单结清(已还)
            return @"Fecha de reembolso";
        }
            break;
            
        default:
        {
            return @"";
        }
            break;
    }
}


#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;//样式
        [self setUpView];
    }
    return self;
}

- (void)setUpView{
    [self.contentView addSubview:self.viewBack];
    [self.viewBack addSubview:self.imageLogo];
    [self.viewBack addSubview:self.labelState];
    [self.viewBack addSubview:self.labelTitle];
    [self.viewBack addSubview:self.labelMoneyTitle];
    [self.viewBack addSubview:self.labelMoney];
    [self.viewBack addSubview:self.labelTimeTitle];
    [self.viewBack addSubview:self.labelTime];
    [self.viewBack addSubview:self.labelError];
    [self.viewBack addSubview:self.labelHint];
    [self.viewBack addSubview:self.btnEdit];
}


#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    [NSObject clickFeedback];
    [sender addClickAnimate];
    switch (sender.tag) {
        case 1:
        {
            [Global_Nav pushVCName:@"WXPBankCardVC" animated:true];
        }
            break;
            
        default:
            break;
    }
}


#pragma mark - 刷新cell
- (void)resetCellWithModel:(ModelOrder *)model{
    self.orderModel = model;
    self.imageLogo.leftTop = XY(LEFT_MARDIN, LEFT_MARDIN);
    [self.imageLogo sd_setImageWithUrlStr:readStr(model.slogan) placeholderImage:@""];
    
    // 169、110 直显示文字，不带箭头
    self.labelState.textColor = self.stateColor;
    if(model.status.integerValue == 169 || model.status.integerValue  == 110){
        [self.labelState fitTitle:readStr(self.stateStr) variable:0];
    }else{
        [self.labelState fitTitle:[NSString stringWithFormat:@"%@>",readStr(self.stateStr)] variable:0];
    }
    self.labelState.backgroundColor = self.stateBackColor;
    self.labelState.widthHeight = XY(self.labelState.width+2*LEFT_MARDIN, 20);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.labelState.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = path.CGPath;
    self.labelState.layer.mask = maskLayer;
    self.labelState.rightCenterY = XY(self.viewBack.width, self.imageLogo.centerY);
    
    // 产品名称
    [self.labelTitle fitTitle:readStr(model.productName) variable:self.labelState.left-self.imageLogo.right-12];
    self.labelTitle.leftCenterY = XY(self.imageLogo.right+12, self.imageLogo.centerY);
    
    [self.labelMoneyTitle fitTitle:@"Importe del préstamo" variable:0];
    if([self.orderModel.status integerValue] == 200){
        [self.labelMoneyTitle fitTitle:@"Monto de reembolso" variable:0];
    }
    self.labelMoneyTitle.leftTop = XY(self.imageLogo.left, self.imageLogo.bottom+LEFT_MARDIN);
   
    [self.labelMoney fitTitle:[NSString stringWithFormat:@"$ %@", self.moneyStr] variable:0];
    self.labelMoney.leftTop = XY(self.labelMoneyTitle.left, self.labelMoneyTitle.bottom+8);
    
    [self.labelTimeTitle fitTitle:self.timeTitleStr variable:0];
    self.labelTimeTitle.rightCenterY = XY(self.viewBack.width-self.imageLogo.left, self.labelMoneyTitle.centerY);
    
    [self.labelTime fitTitle:readStr(self.timeStr) variable:0];
    self.labelTime.rightCenterY = XY(self.labelTimeTitle.right, self.labelMoney.centerY);
    
    CGFloat viewBottom = self.labelMoney.bottom+LEFT_MARDIN;
    
    self.labelError.hidden = [self.orderModel.status integerValue] != 180;
    self.labelError.leftTop = XY(LEFT_MARDIN, self.labelMoney.bottom+LEFT_MARDIN);
    if(self.labelError.hidden == false){
        viewBottom = self.labelError.bottom+16;
    }
    
    self.labelHint.hidden = [self.orderModel.status integerValue] != 169;
    self.btnEdit.hidden = self.labelHint.hidden;
    self.btnEdit.centerXTop = XY(self.viewBack.width/2, self.labelMoney.bottom+LEFT_MARDIN);
    self.labelHint.leftTop = XY(LEFT_MARDIN, self.btnEdit.bottom+12);
    if(self.labelHint.hidden == false){
        viewBottom = self.labelHint.bottom+16;
    }
    
    self.viewBack.height = viewBottom;
    self.viewBack.centerXTop = XY(SCREEN_WIDTH/2, 0);
    self.height = self.viewBack.bottom+LEFT_MARDIN;
}
@end
